package com.uw.tookit;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

public class UWToolkitActivity extends Activity {
	/**
	 * This Activity loads the front page of the application. 
	 */
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // add listeners
        addListenenerOnSearchBox();
        addListenenerOnGridItems();
    }
    
    /**
     * Create the options menu when first load the app.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    /**
     * Handle the event when a menu item is selected.
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.map_button:
                showMap();
                return true;
            case R.id.category_button:
                showCategory();
                return true;
            case R.id.about_button:
            	showAbout();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    /**
     * An action listener is placed on the search box shown on the front page. When the user presses
     * "Enter" after typing something on the search box, this function calls the ResultActivity and 
     * runs it.
     */
    public void addListenenerOnSearchBox() {
    	final EditText edittext = (EditText) findViewById(R.id.entry);
    	edittext.setOnKeyListener(new OnKeyListener() {
	        public boolean onKey(View v, int keyCode, KeyEvent event) {
	            // If the event is a key-down event on the "enter" button
	            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
	                (keyCode == KeyEvent.KEYCODE_ENTER)) {
	            	if (edittext.getText().toString().equalsIgnoreCase("")) {
	            		return true;
	            	}
	            	// Perform action on key press
	            	ArrayList<String> paramsNames = new ArrayList<String>();
		        	ArrayList<String> paramsValues = new ArrayList<String>();
		        	paramsNames.add("q");
		        	paramsValues.add(edittext.getText().toString().trim());
		        	// Create the results activity, and pass on the parameter-value parameters for it to query.
		        	Intent resultsPage = new Intent(UWToolkitActivity.this, Results.class);
		            resultsPage.putStringArrayListExtra("paramNames", paramsNames);
		            resultsPage.putStringArrayListExtra("paramValues", paramsValues);
		            startActivityForResult(resultsPage, 0);
	              return true;
	            }
	            return false;
	        }
	    });
    }
    
    /**
     * An action listener is placed on each grid item on the application page. When an grid item (category) is pressed,
     * it will display a list of items inside that category.
     */
    public void addListenenerOnGridItems() {
    	GridView gridview = (GridView) findViewById(R.id.gridview);
	    gridview.setAdapter(new ImageAdapter(this));

	    // grid item listeners
	    gridview.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	        	String[] gridIconNames = ImageAdapter.getMTextIDs();
	        	String gridName = gridIconNames[position];
	            Toast.makeText(UWToolkitActivity.this, "" + gridName, Toast.LENGTH_SHORT).show();
	            // Create the parameter-value strings which need to be passed onto the server.
	        	ArrayList<String> paramsNames = new ArrayList<String>();
	        	ArrayList<String> paramsValues = new ArrayList<String>();
	        	paramsNames.add("category");
	        	paramsValues.add(gridName);
	        	// Create the results activity, and pass on the parameter-value parameters for it to query.
	        	Intent resultsPage = new Intent(UWToolkitActivity.this, Results.class);
	            resultsPage.putStringArrayListExtra("paramNames", paramsNames);
	            resultsPage.putStringArrayListExtra("paramValues", paramsValues);
	            startActivityForResult(resultsPage, 0);
	        }
	    });
    }
    
    /**
     * Display the Google map centered at Red Square when map button is presses.
     */
    public void showMap() {
    	Intent resultsPage = new Intent(UWToolkitActivity.this, CampusMap.class);  
        startActivityForResult(resultsPage, 0);
    }
    
    /**
     * Display the category list when category button is pressed.
     */
    public void showCategory() {
    	Intent resultsPage = new Intent(UWToolkitActivity.this, CategoriesActivity.class);  
        startActivityForResult(resultsPage, 0);
    }
    
    /**
     * Display the About Us section.
     */
    public void showAbout() {
    	Intent resultsPage = new Intent(UWToolkitActivity.this, About.class);  
        startActivityForResult(resultsPage, 0);
    }
    
}